package cn.csg.iotplatform.module.system.service.security;

import cn.csg.iotplatform.framework.common.util.crypto.RSAUtil;
import cn.csg.iotplatform.module.system.dal.dataobject.security.SecurityConfigDO;
import cn.csg.iotplatform.module.system.dal.mysql.security.SecurityConfigMapper;
import cn.csg.iotplatform.module.system.enums.security.SecurityStatusEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.csg.iotplatform.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csg.iotplatform.module.system.enums.ErrorCodeConstants.*;

@Service
public class SecurityConfigServiceImpl implements SecurityConfigService{

    @Resource
    private SecurityConfigMapper securityConfigMapper;

    @Override
    public SecurityConfigDO getByClientId(String clientId) {
        return securityConfigMapper.selectOne(SecurityConfigDO::getClientId,clientId);
    }

    @Override
    public String decrypt(String content, String clientId) {
        SecurityConfigDO securityConfigDO = getByClientId(clientId);
        if(securityConfigDO == null) throw exception(CLIENT_ID_NOT_EXISTS);
        if(SecurityStatusEnum.isDisable(securityConfigDO.getStatus())) throw exception(CLIENT_ID_NOT_DISABLE);
        return RSAUtil.decrypt(content,securityConfigDO.getPrivateKey());
    }
}
